import { getToken, setToken, removeToken, setUserId, removeUserId } from '@/utils/auth'

const user = {
  state: {
    currentToken: getToken(),
    userId: null
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      // state.token = token;
      state.currentToken = token
      // 获取到token的30分钟之后
      // state.tokenNextUpdate = new Date(new Date().getMinutes() + 30);
    },
    SET_USER_ID: (state, userId) => {
      state.userId = userId
    }
  },

  actions: {
    // 用户名登录成功之后保存用户信息和token
    SetUser (context, userInfo) {
      console.log(userInfo)
      context.commit('SET_USER_ID', userInfo.info.userId)
      context.commit('SET_TOKEN', userInfo.access_token)
      setToken(userInfo.access_token)
      setUserId(userInfo.info.userId)
    },
    // 前端 登出
    FedLogOut ({ commit }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_USER_ID', '')
        removeToken()
        removeUserId()
        resolve()
      })
    },
    // remove token
    resetToken ({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_USER_ID', '')
        removeToken()
        removeUserId()
        resolve()
      })
    }
  }
}

export default user
